﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net;
namespace Project.Messaging
{
    /// <summary>
    /// 默认发消息服务
    /// </summary>
    public class EmailMessageManager : IMessageManager
    {

        #region Delegate & Event
        /// <summary>
        /// 加入发送队列中
        /// </summary>
        /// <param name="context"></param>
        public delegate void SendingEventHandler(MessageContext context);
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="context"></param>
        public delegate void SentEventHandler(MessageContext context);
        /// <summary>
        /// 加入发送队列中
        /// </summary>
        public event SendingEventHandler Sending;
        /// <summary>
        /// 发送消息
        /// </summary>
        public event SentEventHandler Sent;

        void OnSending(MessageContext context)
        {
            if (Sending != null)
                Sending(context);
        }
        void OnSent(MessageContext context)
        {
            if (Sent != null)
                Sent(context);
        }
        #endregion

        #region IMessageManager 成员

        public void Send(string recipient, MessageType type, string subject, string body)
        {
            Send(new List<string> { recipient }, type, subject, body);
        }

        public void Send(IEnumerable<string> recipients, MessageType type, string subject, string body)
        {
            Send(recipients, type, subject, body, false);
        }

        public void Send(IEnumerable<string> recipients, MessageType type, string subject, string body, bool isAsync)
        {
            if (recipients != null && recipients.Any())
            {
                using (SmtpClient client = new SmtpClient()
                {
                    Host = MessageConfig.Instance.Host,
                    Port = MessageConfig.Instance.Port,
                    Credentials = new NetworkCredential(MessageConfig.Instance.UserName, MessageConfig.Instance.Password),
                    EnableSsl = false,//设置为true会出现"服务器不支持安全连接的错误"
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                })
                {
                    #region Send Message
                    var mail = new MailMessage
                    {
                        From = new MailAddress(MessageConfig.Instance.Address, MessageConfig.Instance.DisplayName),
                        Subject = subject,
                        Body = body,
                        IsBodyHtml = true,
                    };
                    MailAddressCollection mailAddressCollection = new MailAddressCollection();
                    recipients.ToList().ForEach(i =>
                    {
                        mail.To.Add(i);
                    });
                    if (isAsync)
                    {
                        client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
                        client.SendAsync(mail, recipients);
                    }
                    else
                    {
                        client.Send(mail);
                    }
                    #endregion
                }
            }
        }

        void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            string arr = null;
            (e.UserState as List<string>).ToList().ForEach(i => { arr += i; });
            //发送完成后要做的事件，可能是写日志
        }

        #endregion
    }
}
